<?php

namespace app\api\service;

use Illuminate\Support\Str;

class OssService
{
    public static function previewUrl($file, $file_name = '未命名文件')
    {
        $file_arr = explode('/', $file);
        $file_full_name = array_pop($file_arr);
        [$file_hash, $file_ext] = explode('.', $file_full_name);
        $title = $file_name . '.' . $file_ext;
        $callback = url('api/file/saveBack', '', '', true) . '?file_name=' . $file;
        !Str::startsWith('http', $file) && $file = config('oss_url') . $file;
        $uid = cookie('user_id') ?: 0;
        $nickName = cookie('user_name') ?: '未登录';
        $avatar = cookie('user_avatar') ?: 'https://mk.deaigz.com/static/admin/images/profile_small.jpg';
        $json = <<<JSON
        { "doc": { "docId": "{$file_hash}", "title": "{$title}", "fetchUrl": "{$file}", "callback": "{$callback}" }, "user": { "uid": "{$uid}", "nickName": "$nickName", "avatar": "$avatar" } }
        JSON;
        $base_url = config('office_url');
        $data_base64 = base64_encode($json);
        return $base_url . '/apps/editor/openEditor?data=' . $data_base64;
    }
}
